`include "para_def.v"
`timescale 1ns / 1ps
`define PMAP_LEN 50
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    15:47:22 07/22/2013 
// Design Name: 
// Module Name:    top 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module top(
    );
	 
	reg			clk;
	reg			rst_n;
	wire	[7:0]	indata;
	reg			invalid;
	wire	[7:0]	data_1_2;
	wire			valid_1_2;
	wire			msg_end_1_2;
	wire	[7:0]	data_2_3;
	wire			valid_2_3;
	wire  [`PMAP_LEN - 1:0] sel_2_3;
	wire	[3:0] field_counter_2_3;
	wire			msg_end_2_3;
	
	reg	[7:0] data	[0:100];
	integer		counter;
	
		wire msg_end_3_4;
	wire [`ID_0_LEN*8 - 1  : 0]			ID_0;
wire [`ID_1_LEN*8 - 1  : 0]			ID_1;
wire [`ID_2_LEN*8 - 1  : 0]			ID_2;
wire [`ID_3_LEN*8 - 1  : 0]			ID_3;
wire [`ID_4_LEN*8 - 1  : 0]			ID_4;
wire [`ID_5_LEN*8 - 1  : 0]			ID_5;
wire [`ID_6_LEN*8 - 1  : 0]			ID_6;
wire [`ID_7_LEN*8 - 1  : 0]			ID_7;
wire [`ID_8_LEN*8 - 1  : 0]	 		ID_8;
wire [`ID_9_LEN*8 - 1  : 0]			ID_9;
wire [`ID_10_LEN*8 - 1  : 0]			ID_10;
wire [`ID_11_LEN*8 - 1  : 0]			ID_11;
wire [`ID_12_LEN*8 - 1  : 0]			ID_12;
wire [`ID_13_LEN*8 - 1  : 0]			ID_13;
wire [`ID_14_LEN*8 - 1  : 0]			ID_14;
wire [`ID_15_LEN*8 - 1  : 0]			ID_15;
wire [`ID_16_LEN*8 - 1  : 0]			ID_16;
wire [`ID_17_LEN*8 - 1  : 0]			ID_17;
wire [`ID_18_LEN*8 - 1  : 0]	 		ID_18;
wire [`ID_19_LEN*8 - 1  : 0]			ID_19;
wire [`ID_20_LEN*8 - 1  : 0]			ID_20;
wire [`ID_21_LEN*8 - 1  : 0]			ID_21;
wire [`ID_22_LEN*8 - 1  : 0]			ID_22;
wire [`ID_23_LEN*8 - 1  : 0]			ID_23;
wire [`ID_24_LEN*8 - 1  : 0]			ID_24;
wire [`ID_25_LEN*8 - 1  : 0]			ID_25;
wire [`ID_26_LEN*8 - 1  : 0]			ID_26;
wire [`ID_27_LEN*8 - 1  : 0]			ID_27;
wire [`ID_28_LEN*8 - 1  : 0]	 		ID_28;
wire [`ID_29_LEN*8 - 1  : 0]			ID_29;
wire [`ID_30_LEN*8 - 1  : 0]			ID_30;
wire [`ID_31_LEN*8 - 1  : 0]			ID_31;
wire [`ID_32_LEN*8 - 1  : 0]			ID_32;
wire [`ID_33_LEN*8 - 1  : 0]			ID_33;
wire [`ID_34_LEN*8 - 1  : 0]			ID_34;
wire [`ID_35_LEN*8 - 1  : 0]			ID_35;
wire [`ID_36_LEN*8 - 1  : 0]			ID_36;
wire [`ID_37_LEN*8 - 1  : 0]			ID_37;
wire [`ID_38_LEN*8 - 1  : 0]	 		ID_38;
wire [`ID_39_LEN*8 - 1  : 0]			ID_39;
wire [`ID_40_LEN*8 - 1  : 0]			ID_40;
wire [`ID_41_LEN*8 - 1  : 0]			ID_41;
wire [`ID_42_LEN*8 - 1  : 0]			ID_42;
wire [`ID_43_LEN*8 - 1  : 0]			ID_43;
wire [`ID_44_LEN*8 - 1  : 0]			ID_44;
wire [`ID_45_LEN*8 - 1  : 0]			ID_45;
wire [`ID_46_LEN*8 - 1  : 0]			ID_46;
wire [`ID_47_LEN*8 - 1  : 0]			ID_47;
wire [`ID_48_LEN*8 - 1  : 0]	 		ID_48;
wire [`ID_49_LEN*8 - 1  : 0]			ID_49;
	wire valid;
	wire [63:0] data_out;
	
stage1 field_sender(
	.valid_0_1(invalid),
	.data_0_1(indata),
	.valid_1_2(valid_1_2),
	.data_1_2(data_1_2),
	.msg_end_1_2(msg_end_1_2),
	
	.clk(clk),
	.rst(~rst_n)	//P RST
);

field_spliter field_spliter(
	.rst_n(rst_n),
   .clk(clk),
   .valid_1_2(valid_1_2),
   .data_1_2(data_1_2),
   .msg_end_1_2(msg_end_1_2),
   .valid_2_3(valid_2_3),
   .data_2_3(data_2_3),
   .sel_2_3(sel_2_3),
   .field_counter_2_3(field_counter_2_3),
   .msg_end_2_3(msg_end_2_3)
);	

stage3 s3 (
		.clk(clk), 
		.rst_n(rst_n), 
		.valid_2_3(valid_2_3), 
		.sel_2_3(sel_2_3), 
		.field_counter_2_3(field_counter_2_3), 
		.data_2_3({1'b0,data_2_3[6:0]}), 
		.msg_end_2_3(msg_end_2_3), 
		.msg_end_3_4(msg_end_3_4), 
		.ID_0(ID_0), 
		.ID_1(ID_1), 
		.ID_2(ID_2), 
		.ID_3(ID_3), 
		.ID_4(ID_4), 
		.ID_5(ID_5), 
		.ID_6(ID_6), 
		.ID_7(ID_7), 
		.ID_8(ID_8), 
		.ID_9(ID_9), 
		.ID_10(ID_10), 
		.ID_11(ID_11), 
		.ID_12(ID_12), 
		.ID_13(ID_13), 
		.ID_14(ID_14), 
		.ID_15(ID_15), 
		.ID_16(ID_16), 
		.ID_17(ID_17), 
		.ID_18(ID_18), 
		.ID_19(ID_19), 
		.ID_20(ID_20), 
		.ID_21(ID_21), 
		.ID_22(ID_22), 
		.ID_23(ID_23), 
		.ID_24(ID_24), 
		.ID_25(ID_25), 
		.ID_26(ID_26), 
		.ID_27(ID_27), 
		.ID_28(ID_28), 
		.ID_29(ID_29), 
		.ID_30(ID_30), 
		.ID_31(ID_31), 
		.ID_32(ID_32), 
		.ID_33(ID_33), 
		.ID_34(ID_34), 
		.ID_35(ID_35), 
		.ID_36(ID_36), 
		.ID_37(ID_37), 
		.ID_38(ID_38), 
		.ID_39(ID_39), 
		.ID_40(ID_40), 
		.ID_41(ID_41), 
		.ID_42(ID_42), 
		.ID_43(ID_43), 
		.ID_44(ID_44), 
		.ID_45(ID_45), 
		.ID_46(ID_46), 
		.ID_47(ID_47), 
		.ID_48(ID_48), 
		.ID_49(ID_49)
	);
	
	stage4 s4 (
		.clk(clk), 
		.rst_n(rst_n), 
		.msg_end_3_4(msg_end_3_4), 
		.ID_0(ID_0), 
		.ID_1(ID_1), 
		.ID_2(ID_2), 
		.ID_3(ID_3), 
		.ID_4(ID_4), 
		.ID_5(ID_5), 
		.ID_6(ID_6), 
		.ID_7(ID_7), 
		.ID_8(ID_8), 
		.ID_9(ID_9), 
		.ID_10(ID_10), 
		.ID_11(ID_11), 
		.ID_12(ID_12), 
		.ID_13(ID_13), 
		.ID_14(ID_14), 
		.ID_15(ID_15), 
		.ID_16(ID_16), 
		.ID_17(ID_17), 
		.ID_18(ID_18), 
		.ID_19(ID_19), 
		.ID_20(ID_20), 
		.ID_21(ID_21), 
		.ID_22(ID_22), 
		.ID_23(ID_23), 
		.ID_24(ID_24), 
		.ID_25(ID_25), 
		.ID_26(ID_26), 
		.ID_27(ID_27), 
		.ID_28(ID_28), 
		.ID_29(ID_29), 
		.ID_30(ID_30), 
		.ID_31(ID_31), 
		.ID_32(ID_32), 
		.ID_33(ID_33), 
		.ID_34(ID_34), 
		.ID_35(ID_35), 
		.ID_36(ID_36), 
		.ID_37(ID_37), 
		.ID_38(ID_38), 
		.ID_39(ID_39), 
		.ID_40(ID_40), 
		.ID_41(ID_41), 
		.ID_42(ID_42), 
		.ID_43(ID_43), 
		.ID_44(ID_44), 
		.ID_45(ID_45), 
		.ID_46(ID_46), 
		.ID_47(ID_47), 
		.ID_48(ID_48), 
		.ID_49(ID_49), 
		.valid(valid), 
		.data_out(data_out)
	);
	
	initial 
	begin
		clk		=	0;
		rst_n		=	1;
		invalid	=	0;
		counter	=	0;
		$readmemh("data.txt",data);
		#12	rst_n	=	0;
		#10	rst_n	=	1;
		
		#10	invalid = 1;
		 
		
		
	end
	
	always #5	clk	=	~clk;

	always @(posedge clk)
	begin
		if((rst_n)&invalid)
			counter	<=	counter + 1;
	end
	
	assign indata	=	data[counter];

endmodule
